package com.example.baimao_tp.data.model

import android.util.Log
import com.example.baimao_tp.data.ReferenceImage
import com.google.gson.annotations.SerializedName

data class MediaResponse(
    val code: Int,
    val msg: String,
    val data: MediaData
)

data class MediaData(
    val list: List<MediaItem>?,
    val page: Int?,
    val page_size: Int?,
    val total: Int?
)

// 多语言描述数据类
data class Description(
    val en: String? = null,
    val cn: String? = null
) {
    // 根据语言代码获取描述，默认返回英文
    fun getDescription(language: String = "en"): String {
        return when (language.lowercase()) {
            "zh", "cn" -> cn ?: en ?: ""
            "en" -> en ?: cn ?: ""
            else -> en ?: cn ?: ""
        } ?: ""
    }
}

data class MediaItem(
    val id: Int,
    val filename: String,
    val url: String,
    val size: Int,
    val type: String,
    val category: String,
    val description: Description, // 改为Description对象
    val status: String,
    val tags: String?, // 改为字符串类型
    @SerializedName("created_at")
    val createdAt: String,
    @SerializedName("updated_at")
    val updatedAt: String
) {
    // 转换为ReferenceImage的扩展函数，支持语言参数
    fun toReferenceImage(base_url: String = "https://baimao-hz.cn", language: String = "en"): ReferenceImage {
        val TAG = "MediaItem"

        // URL拼接日志
        val finalUrl = base_url + url
        Log.d(TAG, "URL拼接: base_url='$base_url' + url='$url' = '$finalUrl'")

        // 多语言描述处理
        val finalDescription = description.getDescription(language).ifEmpty { filename }
        Log.d(TAG, "描述处理: 语言='$language', 原始描述='$description', 文件名='$filename', 最终='$finalDescription'")

        // 标签处理日志 - 适配新的tags字符串格式
        val finalTips = if (!tags.isNullOrBlank()) {
            "标签: $tags"
        } else {
            "无特殊提示"
        }
        Log.d(TAG, "标签处理: tags='$tags', tips='$finalTips'")

        val referenceImage = ReferenceImage(
            id = id.toString(),
            imageUrl = finalUrl,
            description = finalDescription,
            poseTips = finalTips
        )

        Log.d(TAG, "数据转换完成: id=${referenceImage.id}, imageUrl=${referenceImage.imageUrl}, description=${referenceImage.description}")
        return referenceImage
    }
}